﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;

namespace Survey.Entity
{
    /// <summary>
    ///     base implementator of ICodeList represents almost unchanging set of values.
    ///     E.g. Countries, Currencies, Types, Colors.
    ///     In comparison with Enum they are more likely objects - entities
    ///     with uniqe Code and more describing Name.
    ///     Due to their state, ICodeLists are easy to localize
    ///     and therefore used on UI with approprieate user language dependent value
    /// </summary>
    [Serializable]
    public class CodeList : Persistent<int>, ICodeList
    {
        #region members
        bool _isVisible = true;
        #endregion members

        #region ICodeList Members
        /// <summary>
        ///     Unique value, distinguishing entity in the list
        ///     (e.g. 'EUR' fo currency, 'IN' for country
        /// </summary>
        public virtual string Code { get; set; }

        /// <summary>
        ///     Name discribing the Code value in the main
        ///     application language
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        ///     Very important switch, allowing to hide not so often used values.
        ///     CodeList can contain all 180 countries,
        ///     but only few frequently used will be displayed in comboboxes ...
        /// </summary>
        public virtual bool IsVisible
        {
            get { return _isVisible; }
            set { _isVisible = value; }
        }
        #endregion

        #region not persisted
        /// <summary>
        ///     Virtual property (not persisted)
        ///     Provides the localized version of the Name.
        ///     This property is filled on Facade using GetAll(),
        ///     or in the ComboBox.
        ///     Until is set (in runtime) returns the Name property.
        /// </summary>
        public virtual string Localized
        {
            get { return Code.GetLocalized(GetType().Name); }
        }
        #endregion not persisted

        #region override abstract
        /// <summary>
        ///     Code is returned
        /// </summary>
        public override string ToDisplay()
        {
            if (Localized.IsEmpty()
                || Localized.IsEqual(Global.Settings.PrefixForNotFoundTranslation + Code))
            {
                return Name.IsEmpty() ? Code : Name;
            }
            return Localized;
        }

        /// <summary>
        ///     Code must be unique for the ICodeList entity
        /// </summary>
        protected override string GetDomainObjectSignature()
        {
            return Code;
        }
        #endregion override abstract
    }
}